<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>点击</title>
</head>
<body onload="main()">
<canvas id="canvas" height="400" width="400">
    你的浏览器不支持WebGL，请更换新的浏览器
</canvas>
</body>

<script type='text/javascript' src='../lib/webgl-utils.js'></script>
<script type='text/javascript' src='../lib/webgl-debug.js'></script>
<script type='text/javascript' src='../lib/cuon-utils.js'></script>
<script>

    var vertextShader = "attribute vec4 a_Position;\n" +
                        "attribute vec4 a_Color;\n" +
                        "varying vec4 v_Color;\n" +
                        "void main(){\n" +
                        "    gl_Position = a_Position;\n" +
                        "    v_Color = a_Color;\n" +
                        "}";
    var fragShader = "precision mediump float;\n" +
                     "varying vec4 v_Color;\n" +
                     "void main(){\n" +
                     "    gl_FragColor = v_Color;\n" +
                     "}";

    function main(){
        var canvas = document.getElementById("canvas");
        var gl = canvas.getContext("webgl");

        if(!gl){
            console.error('webgl not supported!');
            return;
        }

        var initShaderResult = initShaders(gl, vertextShader, fragShader);
        if(!initShaderResult){
            console.error('initShaders was failed!');
            return;
        }

        var n = iniVertexBuffer(gl);

        gl.clearColor(0.0, 0.0, 0.0, 1.0);
        gl.clear(gl.COLOR_BUFFER_BIT);

        gl.drawArrays(gl.TRIANGLE_STRIP, 0, n);
    }

    function iniVertexBuffer(gl) {

        //这里有点问题，后面才知道怎么处理 --> 0823 17:25处理
        var vertexPoints = new Float32Array(
            [
                -0.5, 0.5, 1.0, 0.0, 0.0,  //x,y,r,g,b
                -0.5, -0.5, 0.0, 1.0, 0.0,
                0.5, 0.5, 1.0, 1.0, 0.0,
                0.5, -0.5, 0.0, 0.0, 1.0,
            ]
        );

        // var vertexPoints = new Float32Array(
        //     [
        //         -0.5, 0.5,
        //         -0.5, -0.5,
        //         0.5, 0.5, 
        //         0.5, -0.5
        //     ]
        // );
        var size = vertexPoints.BYTES_PER_ELEMENT;

        var n = 4;

        var vertexBuffer = gl.createBuffer();
        if(!vertexBuffer){
            return -1;
        }
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, vertexPoints, gl.STATIC_DRAW);

        var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
        if(a_Position < 0){
            console.error('a_Position is not defined!');
            return;
        }

        gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, size * 5, 0);
        gl.enableVertexAttribArray(a_Position);

        var a_Color = gl.getAttribLocation(gl.program, 'a_Color');
        if(a_Color < 0){
            console.error('a_Color is not defined!');
            return;
        }

        gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, size * 5, size * 2);
        gl.enableVertexAttribArray(a_Color);

        return n;
    }
</script>